package org.yufan.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yufan.bean.Item;
import org.yufan.bean.ItemDesc;
import org.yufan.common.Result;
import org.yufan.common.ResultUtils;
import org.yufan.exception.CustomerException;
import org.yufan.service.JedisService;
import org.yufan.service.rpc.ItemRpcService;
import org.yufan.utils.JsonUtils;

@RequestMapping("/item")
@Controller
public class ItemController {

    private Logger LOGGER=LoggerFactory.getLogger(ItemController.class);

    //@Autowired
    //private ItemService itemService;

    @Autowired
    private ItemRpcService itemRpcService;

    @Autowired
    private JedisService jedisService;

    public static final String ITEM_CACHE="item_cache:";
    @RequestMapping(value = "{itemId}",method = RequestMethod.GET)
    public String toItem(@PathVariable("itemId") Long itemId,Model model) throws CustomerException {

        //先从缓存中找，如果没有再去调用后台系统，然后让入缓存
        String json_item = jedisService.get(ITEM_CACHE+itemId);
        Item item=null;
        if(!StringUtils.isEmpty(json_item)){
            LOGGER.info("商品id为{}，从缓存中查询-------------------------",itemId);
            item=JsonUtils.jsonToPojo(json_item,Item.class);
        }else{
            LOGGER.info("商品id为{}，调用后台查询-------------------------",itemId);
            //查询商品基本信息
            item = itemRpcService.queryItemById(itemId);
            //把查询到的数据转成json
            json_item=JsonUtils.objectToJson(item);
            //放入缓存
            jedisService.set(ITEM_CACHE+itemId,json_item);
            //设置失效时间
            jedisService.expire(ITEM_CACHE+itemId,60*60*2);
        }
        LOGGER.debug("查询的商品id为{}",itemId);
        if(item==null){
            LOGGER.error("商品不存在,商品的id为{}",itemId);
            throw  new CustomerException("商品不存在，请联系管理员!");
        }
        model.addAttribute("item",item);
        //处理图片
        String image=item.getImage();
        if(StringUtils.isEmpty(image)){
            LOGGER.error("商品的图片不存在,商品的id为{}",itemId);
            throw new CustomerException("图片不存在，请联系管理员!");
        }

        ItemDesc itemDesc = itemRpcService.queryItemDescById(itemId);
        model.addAttribute("itemDesc",itemDesc);

        //放置图片
        String[] images = image.split(",");
        model.addAttribute("images",images);
        return "item";
    }


    @RequestMapping(value = "/cache/delete/{itemId}")
    @ResponseBody
    public Result deleteItemCache(@PathVariable("itemId") Long itemId){
        jedisService.del(ITEM_CACHE+itemId);
        LOGGER.info("删除了{}商品的缓存------------------------",itemId);
        return ResultUtils.buildSuccess();
    }

}
